package rollercoaster.train.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import rollercoaster.train.interfaces.VagoneDataReader;

public class XMLVagoneDataReader implements VagoneDataReader {

	private HashMap<String, List<String>> textureInfo=new HashMap<String, List<String>>();

	public XMLVagoneDataReader(String filepath) {
		super();
		readXML(filepath);
	}

	private void readXML(String filepath){
		File xmlFile=new File(filepath);
		DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
		org.w3c.dom.Document doc=null;
		
		try {
			DocumentBuilder builder=dbFactory.newDocumentBuilder();
			doc=builder.parse(xmlFile);
			doc.getDocumentElement().normalize();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		NodeList textures=doc.getElementsByTagName("texture");
		for (int i = 0; i < textures.getLength(); i++) {
			Element texture=(Element) textures.item(i);
			String texturePath=texture.getAttribute("path");
			NodeList surfaces=texture.getElementsByTagName("surf");
			
			List<String> objPathList=new ArrayList<String>();
			for (int j = 0; j < surfaces.getLength(); j++) {
				Element surf=(Element) surfaces.item(j);
				String objpath=surf.getTextContent();
				objPathList.add(objpath);
			}
			
			textureInfo.put(texturePath, objPathList);
		}
		
	}

	public HashMap<String, List<String>> getTextureInfo() {
		return textureInfo;
	}

}
